package base;

/**
 * 1、可以解决线程访问变量可见性问题，禁止指令重排
 * 2、不具有原子性
 * 3、和 synchronized 区别
 */
public class VolatileTest extends Thread {

    //测试 boolean 类型
    private volatile boolean isRunning = true;
    private boolean isRunning3 = true;

    public boolean isRunning() {//获取锁的时候cpu缓存失效，去主内存获取缓存
        return isRunning;
    }//释放锁时将cpu同步到主内存

    public void setRunning(boolean isRunning) {
        this.isRunning3 = isRunning;
        this.isRunning = isRunning;
    }


    public void run() {
        System.out.println("进入了run...............");
        while (isRunning) {
        }
        System.out.println("isRunning的值被修改为为false,线程将被停止了");
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileTest volatileThread = new VolatileTest();
        volatileThread.start();
        Thread.sleep(2000);

        System.out.println("修改完成");
    }
}